package com.example.userlogin.service;

//实现类

import com.example.userlogin.dao.EmployeeDao;
import com.example.userlogin.dao.PurchaseDao;
import com.example.userlogin.entity.Employee;
import com.example.userlogin.entity.Purchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

//实现操作方法
public class EmployeeService implements EmployeeDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Employee employee) {
//        创建SQL语句
        String sql = "insert into employee values(null,?,?,?,?,?,?)";
//        调用方法实现
        Object[] args = {employee.getName(),employee.getTelephone(),employee.getHiretime(),employee.getJobtitile(),
        employee.getDepartment(),employee.getSalary()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(Employee employee) {
        String sql ="delete from employee where Id=?";
        Object[] args={employee.getId()};
        return jdbcTemplate.update(sql,args);
    }


    @Override
    public int update(Employee employee) {
        String sql ="update employee set name=?,telephone=?,hiretime=?,jobtitile=?,department=?,salary=? where Id=?";
        Object[] args={
                employee.getName(),employee.getTelephone(),employee.getHiretime(),
                employee.getJobtitile(),employee.getDepartment(),employee.getSalary(),employee.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    /**
     *
     * @param
     * @return
     */
    @Override
    public Employee findOne(Employee employee) {
        String sql = "select * from employee where Id="+employee.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Employee>(Employee.class));
    }

    /**
     * 获取所有商品列表的方法
     * @return
     */
    @Override
    public List<Employee> findAll() {
        String sql = "select * from employee";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Employee>(Employee.class));
    }
}
